package cn.unknowpupil.controller.system;

import cn.unknowpupil.common.core.annotation.Log;
import cn.unknowpupil.common.core.enums.BusinessType;
import cn.unknowpupil.common.core.resopnse.ResultResponse;
import cn.unknowpupil.common.security.dto.LoginUser;
import cn.unknowpupil.common.security.utils.SecurityUtils;
import cn.unknowpupil.domain.system.SysMenu;
import cn.unknowpupil.service.system.ISysMenuService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created at 2020-10-21 by  w10g <br>
 * Initialize SysMenu  ... <br>
 *  菜单权限表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-菜单权限表模块" }, value = "SysMenu")
@RestController
@RequestMapping(value = "admin/sys-menu")
public class SysMenuController {

    @Resource
    private ISysMenuService SysMenuService;

    /**
     * 新增
     *
     * @param menu {@link SysMenu}  菜单权限表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增")
    @Log(title = "create", businessType = BusinessType.INSERT)
    @PostMapping("create")
    public ResultResponse create(@Validated @RequestBody SysMenu menu) {

        // 业务逻辑
        boolean created = SysMenuService.create(menu);
        if (created) {
             return ResultResponse.success("创建成功");
        }else{
             return ResultResponse.buildFailure("创建失败");
        }

    }

    /**
     * 修改
     *
     * @param SysMenu {@link SysMenu}  菜单权限表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改")
    @Log(title = "update", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public ResultResponse update(@Validated @RequestBody SysMenu SysMenu) {
        // 业务逻辑
        boolean update = SysMenuService.update(SysMenu);
        if (update) {
             return ResultResponse.success("修改成功");
        }else{
             return ResultResponse.buildFailure("修改失败");
        }

    }

    /**
     * 获取 根据菜单编号获取详细信息
     *
     * @param SysMenuId {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("get/{SysMenuId}")
    public ResultResponse get(@PathVariable("SysMenuId") Long SysMenuId) {
        SysMenu SysMenu = SysMenuService.info(SysMenuId);
        return ResultResponse.buildSuccess(SysMenu);
    }

    /**
     * 删除
     *
     * @param SysMenuId {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除")
    @Log(title = "remove", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{SysMenuId}")
    public ResultResponse remove(@PathVariable("SysMenuId") Long SysMenuId) {
        // 业务逻辑
        boolean deleted  = SysMenuService.delete(SysMenuId);
        if(deleted){
            return ResultResponse.success("删除成功");
        }else{
            return ResultResponse.buildFailure("删除失败");
        }
    }


    @ApiOperation("系统：获取菜单列表")
    @GetMapping("list")
    public ResultResponse list( @ModelAttribute SysMenu menu) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysMenu> menus = SysMenuService.selectMenuList(menu, loginUser.getId());
        return ResultResponse.buildSuccess(menus);
    }

    /**
     * 获取菜单下拉树列表
     */

    @ApiOperation("系统：获取菜单下拉树列表")
    @GetMapping("/tree")
    public ResultResponse tree(SysMenu menu)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysMenu> menus = SysMenuService.selectMenuList(menu, loginUser.getId());
        return ResultResponse.buildSuccess(SysMenuService.buildMenuTreeSelect(menus));
    }
    /**
     * 加载对应角色菜单列表树
     */
    @ApiOperation("系统：加载对应角色菜单列表树")
    @GetMapping("/roleMenuTree/{roleId}")
    public ResultResponse roleMenuTree(@PathVariable("roleId") Long roleId)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysMenu> menus = SysMenuService.selectMenuList(loginUser.getId());
        Map<String,Object> result = Maps.newHashMap();
        // 检查关键词
        result.put("checkedKeys", SysMenuService.selectMenuListByRoleId(roleId));
        result.put("menus", SysMenuService.buildMenuTreeSelect(menus));

        return ResultResponse.buildSuccess(result);
    }
}
